//
//  OnePsychologyViewController.m
//  LCOnePsychology
//
//  Created by li cheng on 16/8/28.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "OnePsychologyViewController.h"
#import "TableViewOfOnePsychology.h"
#import "Album.h"
#import "MJMacro.h"
#import "BaseView.h"
#import "BaseCollectionView.h"
#import "TVCellOfOnePsychology.h"
#import "CVCellOfChioceButton.h"
#import "BaseButton.h"
#import "Masonry.h"
#import "UINavigationBar+category.h"
#import "TVCellOfFindSolveWay.h"
#import "SearchViewController.h"
#import "ZWYNetTool.h"
#import "LCHotRecommendRootClass.h"
#import "LCDataOfHotRecommend.h"
#import "LCUserOfHotRecommend.h"
#import "LCArticleOfHotRecommend.h"
#import "UIImageView+WebCache.h"
#import "VCOfBannersWeb.h"
#import "VCOfReadArticle.h"
#import "VCOfDoTest.h"
#import "VCOfListenFM.h"
#import "VCOfBannersWeb.h"
#import "BaseNavigationController.h"
#import "MJRefresh.h"
@interface OnePsychologyViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSArray *nameArray;
@property (nonatomic, strong) UIView *strip;
@property (nonatomic, strong) TableViewOfOnePsychology *tableViewOfOnePsychology;
@property (nonatomic, strong) NSMutableArray *urlOfImageArray;
@property (nonatomic, strong) LCHotRecommendRootClass *hotRecommendRootClass;
@property (nonatomic, strong) NSArray *arrayOfChoiceButton;
@property (nonatomic, strong) NSArray *arrayOfChoiceName;
@property (nonatomic, strong) NSMutableArray<LCDataOfHotRecommend *> *mArrayOfData;
@end

@implementation OnePsychologyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 网络数据处理
    [self handleData];
    // 创建新的颜色
    UIColor *color = [UIColor new];
    [self.navigationController.navigationBar alphaNavigationBarView:[color colorWithAlphaComponent:1]];
    // 隐藏 navitionBar 下面的那条黑线, 用这两句代码
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    // 创建 searchBar
    [self createSearchBar];
    // 创建壹心理界面的 tableView
    [self createTableViewOfOnePsychology];
}
#pragma mark - 数据处理
- (void)handleData{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/home-list.json?offset=0&token=0d4eeb8732f9ae2c73401002570ceb93&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=520574e9f124c30ba0d553e3a91eae55&_version=4.0" andBody:@"offset=0&token=0d4eeb8732f9ae2c73401002570ceb93&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=520574e9f124c30ba0d553e3a91eae55&_version=4.0" andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _hotRecommendRootClass = [LCHotRecommendRootClass modelDic:result];
        _mArrayOfData = _hotRecommendRootClass.data.mutableCopy;
        [self.tableViewOfOnePsychology reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    _arrayOfChoiceButton = @[@"Read", @"Test", @"Curriculum", @"FM"];
    _arrayOfChoiceName = @[@"读文章", @"做测试", @"学课堂", @"听FM"];
}
#pragma mark - 创建 searchBar
- (void)createSearchBar{
    UIButton *searchBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [searchBarButton setTintColor:[UIColor whiteColor]];
    [searchBarButton setImage:[UIImage imageNamed:@"Search_16"] forState:UIControlStateNormal];
    [searchBarButton addTarget:self action:@selector(searchBarInterface:) forControlEvents:UIControlEventTouchUpInside];
    [searchBarButton setTitle:@"      输入转件姓名、问题关键词            " forState:UIControlStateNormal];
    searchBarButton.frame = CGRectMake(0, 0, 300, 30);
    searchBarButton.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.330];
    searchBarButton.layer.cornerRadius = 15;
    self.navigationItem.titleView = searchBarButton;
}
#pragma searchBarButton 的点击方法
- (void)searchBarInterface:(UIButton *)button{
    SearchViewController *searchViewController = [[SearchViewController alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:navigation animated:YES completion:^{
        
    }];
}
#pragma mark - 创建 tableView
- (void)createTableViewOfOnePsychology{
    // 创建 tableView
    self.tableViewOfOnePsychology = [[TableViewOfOnePsychology alloc] initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableViewOfOnePsychology.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    [self.view addSubview:_tableViewOfOnePsychology];
    self.tableViewOfOnePsychology.delegate = self;
    self.tableViewOfOnePsychology.dataSource = self;
    [self.tableViewOfOnePsychology registerNib:[UINib nibWithNibName:@"TVCellOfOnePsychology" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfOnePsychology"];
    [self.tableViewOfOnePsychology registerClass:[TVCellOfFindSolveWay class] forCellReuseIdentifier:@"TVCellOfFindSolveWay"];
    // 指定头视图
    BaseView *tableHeaderView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 4 / 9)];
//    tableHeaderView.backgroundColor = [UIColor whiteColor];
    CGFloat widthOfAlbum = SCREEN_WIDTH;
    CGFloat heightOfAlbum = (SCREEN_HEIGHT / 2) * 2 / 3 - 20;
    Album *album = [[Album alloc] initWithFrame:CGRectMake(0, 0, widthOfAlbum, heightOfAlbum)];
    album.backgroundColor = [UIColor whiteColor];
    [tableHeaderView addSubview:album];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat widthOfChioceButtonCollectionView = SCREEN_WIDTH;
    CGFloat heightOfChioceButtonCollectionView = SCREEN_HEIGHT / 9;
    layout.itemSize = CGSizeMake(heightOfChioceButtonCollectionView, heightOfChioceButtonCollectionView);
    CGFloat margin = (widthOfChioceButtonCollectionView - heightOfChioceButtonCollectionView * 4) / 5;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = margin;
    layout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
    BaseCollectionView *chioceButtonCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, heightOfAlbum, widthOfChioceButtonCollectionView, heightOfChioceButtonCollectionView) collectionViewLayout:layout];
    chioceButtonCollectionView.backgroundColor = [UIColor whiteColor];
    [tableHeaderView addSubview:chioceButtonCollectionView];
    chioceButtonCollectionView.delegate = self;
    chioceButtonCollectionView.dataSource = self;
    [chioceButtonCollectionView registerNib:[UINib nibWithNibName:@"CVCellOfChioceButton" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfChioceButton"];
    self.tableViewOfOnePsychology.tableHeaderView = tableHeaderView;
    // 下拉刷新
    self.tableViewOfOnePsychology.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 上拉加载
    self.tableViewOfOnePsychology.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
#pragma mark - 下拉刷新
- (void)loadNewData{
    [self handleData];
    [self.tableViewOfOnePsychology.mj_header endRefreshing];
}
#pragma mark - 上拉加载
- (void)loadMoreData{
    static int i = 5;
    [ZWYNetTool GET:[NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/home-list.json?offset=%d&token=0d4eeb8732f9ae2c73401002570ceb93&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=520574e9f124c30ba0d553e3a91eae55&_version=4.0", i] andBody:@"offset=0&token=0d4eeb8732f9ae2c73401002570ceb93&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=520574e9f124c30ba0d553e3a91eae55&_version=4.0" andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _hotRecommendRootClass = [LCHotRecommendRootClass modelDic:result];
        for (LCDataOfHotRecommend *dataOfHotRecommend in _hotRecommendRootClass.data) {
            [_mArrayOfData addObject:dataOfHotRecommend];
        }
        [self.tableViewOfOnePsychology reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"-------------------0000000000000-----------------------0000000000000------------------0000000000000----------------0000000000000--------------------------------------0000000000000-------------------------------------------------------------------------------0000000000000----------------------------------------------------------0000000000000v-------------------------------------------------------------------------------------------------------------0000000000000---------------------")
    }];
    [self.tableViewOfOnePsychology.mj_footer endRefreshing];
    i = i + 5;
}
#pragma mark - collectionView 代理方法 item 的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
#pragma mark - collectionView 代理方法 cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CVCellOfChioceButton *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfChioceButton" forIndexPath:indexPath];
    [cell.myButton setImage:[UIImage imageNamed:_arrayOfChoiceButton[indexPath.row]] forState:UIControlStateNormal];
    cell.myLabel.text = _arrayOfChoiceName[indexPath.row];
    return cell;
}
#pragma mark - chioceButton collectionView 的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        VCOfReadArticle *vcOfReadArticle = [[VCOfReadArticle alloc] init];
        [self presentViewController:vcOfReadArticle animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 1) {
        VCOfDoTest *vcOfDoTest = [[VCOfDoTest alloc] init];
        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vcOfDoTest];
        [self presentViewController:navigation animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 2) {
        VCOfBannersWeb *vcOfBannersWeb = [[VCOfBannersWeb alloc] init];
        vcOfBannersWeb.stringURL = @"http://yiapi.xinli001.com/v2/yi/gkk-list-page?token=0d4eeb8732f9ae2c73401002570ceb93&_platform=android&_version=4.0";
        BaseNavigationController *navigationController = [[BaseNavigationController alloc] initWithRootViewController:vcOfBannersWeb];
        //    [onePsychologyViewController.navigationController pushViewController:vcOfBannersWeb animated:YES];
        [self presentViewController:navigationController animated:YES completion:^{
            
        }];

    }
    if (indexPath.row == 3) {
        VCOfListenFM *vcOfListenFM = [[VCOfListenFM alloc] init];
        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vcOfListenFM];
        [self presentViewController:navigation animated:YES completion:^{
            
        }];
    }
}
#pragma mark - tableView section footer 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
#pragma mark - tableView section 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    } else {    
        return 44;
    }
}
#pragma mark - tableView section header 内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    } else {
        // 创建一个 View 放在 HeaderInSection 上面
        BaseView *viewOfHeaderInSection = [[BaseView alloc] init];
        viewOfHeaderInSection.backgroundColor = [UIColor whiteColor];
        // 创建 <热门推荐> button
        BaseButton *firstChoiceButton = [BaseButton buttonWithType:UIButtonTypeSystem];
        firstChoiceButton.frame = CGRectMake(0, 0, SCREEN_WIDTH / 5, 44);
        [firstChoiceButton setTitle:@"热门推荐" forState:UIControlStateNormal];
        [firstChoiceButton addTarget:self action:@selector(firstButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [viewOfHeaderInSection addSubview:firstChoiceButton];
        // 创建 <我的订阅> buttton
        BaseButton *secondChoiceButton = [BaseButton buttonWithType:UIButtonTypeSystem];
        secondChoiceButton.frame = CGRectMake(SCREEN_WIDTH / 5, 0, SCREEN_WIDTH / 5, 44);
        [secondChoiceButton setTitle:@"" forState:UIControlStateNormal];
        [secondChoiceButton addTarget:self action:@selector(secondButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [viewOfHeaderInSection addSubview:secondChoiceButton];
        // 创建 <订阅> button
        BaseButton *thirdChoiceButton = [BaseButton buttonWithType:UIButtonTypeRoundedRect];
        thirdChoiceButton.backgroundColor = [UIColor whiteColor];
        [thirdChoiceButton setTitle:@"" forState:UIControlStateNormal];
        [viewOfHeaderInSection addSubview:thirdChoiceButton];
//        thirdChoiceButton.layer.cornerRadius = 15;
//        [thirdChoiceButton.layer setBorderWidth:1.0];
//        [thirdChoiceButton.layer setBorderColor:[UIColor grayColor].CGColor];
        [thirdChoiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 5, 30));
            make.right.equalTo(viewOfHeaderInSection).offset(-10);
            make.centerY.equalTo(viewOfHeaderInSection);
        }];
        // 创建下面随着点击滑动的滑条
        self.strip = [[UIView alloc] initWithFrame:CGRectMake(0, 42, SCREEN_WIDTH / 5, 2)];
        [viewOfHeaderInSection addSubview:_strip];
        _strip.backgroundColor = [UIColor blueColor];
        return viewOfHeaderInSection;
    }
}
#pragma 点击 <热门推荐> button 实现方法
- (void)firstButtonClick:(BaseButton *)button{
    _strip.frame = CGRectMake(0, 42, SCREEN_WIDTH / 5, 2);
}
#pragma 点击 <我的订阅> button 实现方法
- (void)secondButtonClick:(BaseButton *)button{
    _strip.frame = CGRectMake(SCREEN_WIDTH / 5, 42, SCREEN_WIDTH / 5, 2);
}
#pragma mark - tableView 代理方法 row 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
    return (SCREEN_HEIGHT / 2) / 3 * 2;
    } else {
    return SCREEN_HEIGHT / 2;
    }
}
#pragma mark - tableView section 的数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark - tableView 代理方法 row 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else {
        return _mArrayOfData.count;
    }
}
#pragma mark - tableView 代理方法 cell 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        TVCellOfFindSolveWay *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfFindSolveWay" forIndexPath:indexPath];
        return cell;
    } else {
        TVCellOfOnePsychology *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfOnePsychology" forIndexPath:indexPath];
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[_mArrayOfData objectAtIndex:indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"0"]];
        cell.title.text = [_mArrayOfData objectAtIndex:indexPath.row].article.title;
        cell.brief.text = [_mArrayOfData objectAtIndex:indexPath.row].article.brief;
        cell.viewnum.text = [_mArrayOfData objectAtIndex:indexPath.row].article.viewnum;
        if ([[_mArrayOfData objectAtIndex:indexPath.row].object_name  isEqual: @"ceshi"]) {
             cell.object_name.text = @"测试";
        } else if ([[_mArrayOfData objectAtIndex:indexPath.row].object_name  isEqual: @"article"]) {
            cell.object_name.text = @"文章";
        }
        return cell;
    }
}
#pragma mark - scrollView 滑动方法改变 navigationBar 透明度的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //1。拿到y值
    CGFloat contentOffY = scrollView.contentOffset.y;
    UIColor *color = [UIColor whiteColor];
    if (contentOffY > (SCREEN_HEIGHT / 2) * 2 / 3 - 64) {
        [self.navigationController.navigationBar alphaNavigationBarView:[color colorWithAlphaComponent:1]];
    } else {
        [self.navigationController.navigationBar alphaNavigationBarView:[color colorWithAlphaComponent:0]];
    }
}
#pragma mark - 其他
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    // test
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
